<?php
namespace jy_peisong;
defined('BY_JYA') or exit('error');
/*
*/
class notice extends \plugin{
	public function gettpl_data($orderid,$type=0){//type=0表单订单，type=1
		return false;
	}
	/*
		抢单通知
	*/
	public function notice_ordermsg($order=array(),$allusers=array()){
		//新配送任务通知给配送员
		$goods=pdo_get('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		if(!is_array($order['address'])){
			$order['address']=json_decode($order['address'],true);
		}
		$goods['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['goodsid']),'title');
		$address_str=model('core/member')->get_address_str($order['address']);
		$template_id=cfc('account')->get_tpl_template_id('48089','收到客户新订单通知','',array('订单时间','商品名称','客户地址'),'ordermsg','jy_peisong');
		if($_SESSION['wxapp_id']){
			$wxapptemplate_id=cfc('wxapp_api')->get_tpl_template_id('17468',[1,2,3,7],'ordermsg','收到客户新订单通知','jy_peisong');
		}
		$address_str=cfc('tools')->substr($address_str,0,10);
		$message=array(
			array('value'=>date('Y/m/d H:i:s',$order['createtime']),"name" => "time2"),
			array('value'=>$goods['title'].'等商品',"name" => "thing8"),
			array('value'=>$address_str,"name" => "thing5"),
		);
		$wxappmsg=array(
			array('value'=>$order['ordersn']),
			array('value'=>date('Y/m/d H:i:s',$order['createtime'])),
			array('value'=>$goods['title']),
			array('value'=>$address_str),
		);
		$url=custom_url('jy_peisong/peisong_order/',true);
		//file_put_contents(ROOT_D.'/1.txt',$order['id']);
		foreach($allusers as $uv){
			cfc('account')->sendtwoNotice($uv['mid'],$template_id,$message,$url,'ordermsg','jy_peisong',$order['id']);
			if($_SESSION['wxapp_id']){
				$url='/jy_peisong/peisong_order/';
				$template_id=cfc('wxapp_api')->checktplNotice($uv['mid'],$wxapptemplate_id,$wxappmsg,$url,'ordermsg','jy_peisong',$order['id']);
			}
		}
		return '';
	}
	/*
		配送员新任务通知
	*/
	public function notice_orderget($order_id,$url=''){
		//新配送任务通知给配送员
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order_id));
		$order['address']=json_decode($order['address'],true);
		$goods=pdo_get('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order_id));
		$goods['title']=pdo_getcolumn('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goods['goodsid']),'title');
		$address_str=model('core/member')->get_address_str($order['address']);
		$template_id=cfc('account')->get_tpl_template_id('53653','订单派送通知','',array('订单编号','商品','配送地址'),'orderget','jy_peisong');
		if($_SESSION['wxapp_id']){
			$wxapptemplate_id=cfc('wxapp_api')->get_tpl_template_id('17468',[1,2,3,7],'orderget','新配送任务通知','jy_peisong');
		}
		$message=array(
			array('value'=>$order['ordersn'],"name" => "character_string7"),
			array('value'=>$goods['title'],"name" => "thing6"),
			array('value'=>$address_str,"name" => "thing3"),
		);
		$wxappmsg=array(
			array('value'=>$order['ordersn']),
			array('value'=>date('Y/m/d H:i:s',$order['createtime'])),
			array('value'=>$goods['title']),
			array('value'=>$address_str),
		);
		if(!$url){
			$url=custom_url('jy_peisong/worker_order/detail/id='.$order['id'],true);	
		}
		if(method_exists(cfc('account'),'sendtwoNotice')){
			cfc('account')->sendtwoNotice($order['peisong_id'],$template_id,$message,$url,'orderget','jy_peisong',$order_id);
		}
		if($_SESSION['wxapp_id']){
			$url='/jy_peisong/worker_order/detail/index?id='.$order['id'];
			$template_id=cfc('wxapp_api')->checktplNotice($order['peisong_id'],$wxapptemplate_id,$wxappmsg,$url,'orderget','jy_peisong',$order_id);
		}
	}
	public function notice_check_apply($apply,$url=''){
		//审核通过
		$member=$this->get('member',array('id'=>$apply['pid'],'uniacid'=>$_SESSION['uniacid']));
		$template_id=cfc('account')->get_tpl_template_id('OPENTM408250107','申请成功通知');
		$message=array(
			'first'=>array('value'=>'申请成为配送员，审核通过！',"color" => "#4a5077"),
			'keyword1'=>array('value'=>$member['title'],"color" => "#4a5077"),
			'keyword2'=>array('value'=>$member['mobile'],"color" => "#4a5077"),
			'remark'=>array('value'=>'感谢你的使用。',"color" => "#4a5077"),
		);
		if(!$url){
			$url=custom_url('jy_peisong/index/',true);	
		}
		if(method_exists(cfc('account'),'sendtwoNotice')){
			cfc('account')->sendtwoNotice($member['mid'],$template_id,$message,$url,'check_apply','jy_peisong',$apply['id']);
		}
	}
}